/**
 * 
 */
	
var lcm = new Array();
var jsp = "";
var changeArray =  new Array();
var isChange = false;

function loaddata(){
	
	
		var bonusGap = 0.0;
		var bonusPool = 0.0;
		var actualBonus = 0.0;
	
	
		//var allFlag = $("#allFlag").val();
	//	var pno = $("#pagenum").val();
		var month=$("#month").val();
		var bonus=$("#bonus").val();
		var scope=$("#scope").val();
		var ope_fk=$("#ope_id_Lcm").val();
		var score = $("#score").val();
		var classtype = $("#classtype").val();
		var emp_id = $("#emp_id").val();
		var url = pms_url + "lcm/performance/list";
		var params = {month:month,bonus:bonus,ope_fk:ope_fk,score:score,scope:scope,emp_id:emp_id,classtype:classtype,jsp:jsp};
		var obj = load(url,params);//发送请求去服务端ope_fk数据	
		lcm = obj.performance;
		
		
		
/*		var aurl = pms_url + "lcm/performance/getBonusGap";
		var param = {month:month,ope_fk:ope_fk,scope:scope};
		$.ajax({  
	        type : "post",  
	        url : aurl,  
	        data : param,
	        dataType : "json", 
	        async : false,  //同步
	        success : function(data){  
	           data = eval(data);  
		   		if (data.err_msg=="success") {
		   			$("#bonusGap").html(data.result);
				}
				else {
					alert("error");
				}
	        }  
	    });*/
		
		
		var aurl = pms_url + "lcm/performance/getAllBonuses";
		var param = {month:month,ope_fk:ope_fk,scope:scope,jsp:jsp};
		$.ajax({  
	        type : "post",  
	        url : aurl,  
	        data : param,
	        dataType : "json", 
	        async : false,  //同步
	        success : function(data){  
	           data = eval(data);  
		   		if (data.err_msg=="success") {
		   			$("#bonusPool").html(data.result);
		   			bonusPool = data.result;
				}
				else {
					alert("error");
				}
	        }  
	    });
		
		
		var aurl = pms_url + "lcm/performance/getActualBonus";
		var param = {month:month,ope_fk:ope_fk,scope:scope,jsp:jsp};
		$.ajax({  
	        type : "post",  
	        url : aurl,  
	        data : param,
	        dataType : "json", 
	        async : false,  //同步
	        success : function(data){  
	           data = eval(data);  
		   		if (data.err_msg=="success") {
		   			$("#actualBonus").html(data.result);
		   			actualBonus = data.result;
				}
				else {
					alert("error");
				}
	        }  
	    });
		
		var bonusGap = bonusPool - actualBonus;
		$("#bonusGap").html(bonusGap);
	}





function save(){
	
	if(isChange){
		
		var a = new Array();
		var month=$("#month").val();
		var table;
		if(jsp == "ManufacturingJSP"){
			table = document.getElementById("ManufacturingPerformancelist");
		}else{
			table = document.getElementById("EngineeringPerformancelist");
		}
		changeArray.sort();
		var newArray = new Array();
		for(var i = 0; i < changeArray.length; i++){
			if(i > 0){
				if(changeArray[i] != changeArray[i-1])
					newArray.push(changeArray[i]);
			}else{
				newArray.push(changeArray[i]);
			}
		}
		
		 
		$('table tr').each(function(i){
			 
			if($.inArray(i, newArray) != -1){
				var tablelist = new Object();
				tablelist.month = month;
				tablelist.emp_id = $(this).children("td:eq(2)").text();
				if(jsp == "ManufacturingJSP"){
					tablelist.bonus = $(this).children("td:eq(9)").text();
				}else{
					tablelist.bonus = $(this).children("td:eq(10)").text();
				}
				
				a.push(tablelist);
				
			}
			
		});
		
		var obj =JSON.stringify(a);
		
		var aurl = pms_url + "lcm/performance/updateBonus";
		$.ajax({  
	        type : "post",  
	        url : aurl,  
	        data : {param:obj},
	        dataType : "json", 
	        async : false,  //同步
	        success : function(data){  
	           data = eval(data);  
		   		if (data.err_msg=="success") {
		   			if(data.result == "0"){
		   				alert("保存成功");
		   				loaddata();
		   			}else{
		   				alert("error!");
		   			}
				}else {
					alert("error!");
				}
	        }  
	    });
		
		
		
		
		//alert(table.rows[0].cells[2].innerHTML);
		
		/*$('table tr').each(function(trindex,tritem){
        $(tritem).find("td").each(function(tdindex,tditem){
       	 
       	 if(tdindex = $(tritem).find("td").length-1){
       		 a.data.add(tablelist);
       	 }
        });
    });
	*/
		
	
	}else{
		alert("内容无改动，无需保存！");
		return;
	}
	
	isChange = false;
	
}





	function update(){

		var url = pms_url + "lcm/performance/syschron"; 
		var month=$("#month").val();
		var param = {month:month};
		$.ajax({  
	        type : "post",  
	        url : url,  
	        dataType: "json", 
	        data : param, 
	        async : false,  //同步
	        beforeSend: function () {  
        	        // 禁用按钮防止重复提交  
        	        $("#btnSyschron").attr({ disabled: "disabled" });
        	        MaskUtil.mask("正在更新中..."); 
        	        
        	    },  
	        success : function(data){ 
	    			if(data.err_msg=="success"){
	    				MaskUtil.unmask();
	    				alert("更新数据成功！");
	    		}
	        	},
	        complete: function () {  
	            	$("#btnSyschron").removeAttr("disabled");  
	            	MaskUtil.unmask();   
	            },  
        	error: function (data) {  
        	        console.info("error: " + data.responseText);  
        	    } 
	        });
	
	}
	
	
	
	
	
	
	function creatTable(){
		
		if(jsp == "ManufacturingJSP"){
			$("#ManufacturingPerformancelist").find("tbody").html("");
			$.each(lcm, function(index, p) {
			var tr ="<tr role='row'>"+
			"<td>"+(index+1)+"</td>"+
			"<td>"+p.label+"</td>"+
			"<td>"+p.emp_id+"</td>"+
			"<td>"+p.emp_name+"</td>"+
			"<td>"+((p.attendance==null)?"0":p.attendance)+"</td>"+
			"<td>"+((p.capacity==null)?"0":p.capacity)+"</td>"+
			"<td>"+((p.specialty==null)?"0":p.specialty)+"</td>"+
			"<td>"+((p.compatibility==null)?"0":p.compatibility)+"</td>"+
			"<td>"+((p.score==null)?"0":p.score)+"</td>" +
			"<td>"+((p.bonus==null)?"0":p.bonus)+"</td></tr>";
			$("#ManufacturingPerformancelist").find("tbody").append(tr);
		 });			
			
		}else{

			$("#EngineeringPerformancelist").find("tbody").html("");
			$.each(lcm, function(index, p) {
			var tr ="<tr role='row'>"+
			"<td>"+(index+1)+"</td>"+
			"<td>"+p.label+"</td>"+
			"<td>"+p.emp_id+"</td>"+
			"<td>"+p.emp_name+"</td>"+
			"<td>"+((p.attendance==null)?"0":p.attendance)+"</td>"+
			"<td>"+((p.discipline==null)?"0":p.discipline)+"</td>"+
			"<td>"+((p.compatibility==null)?"0":p.compatibility)+"</td>"+
			"<td>"+((p.learns==null)?"0":p.learns)+"</td>"+
			"<td>"+((p.professional==null)?"0":p.professional)+"</td>"+
			"<td>"+((p.score==null)?"0":p.score)+"</td>" +
			"<td>"+((p.bonus==null)?"0":p.bonus)+"</td></tr>";
			$("#EngineeringPerformancelist").find("tbody").append(tr);
		 });			
			
		}
		

	}
	
	
	
	
	
	
	$(function () {
		
		
		jsp = $("#JSP_ID").val();
		
		
		loaddata();
		ManufacturingOrEngineering();
		creatTable();
		
		//TableDatatablesEditable.init();
		
		
		
		$("#ope_id_Lcm").change(function(){
			loaddata();
			ManufacturingOrEngineering();
			creatTable();
		});
		
		
		//查询
		$('#btnSearch').on('click', function () {
			$("#pagenum").val(1);
			loaddata();
			ManufacturingOrEngineering();
			creatTable();
			
		});
		
		//重置
		$("#btnReset").on('click', function () {
			$("#searchform input").val("");
			$("#searchform select").val("");
		});	
	
		//更新
		$("#btnSyschron").on('click', function () {
			update();
			loaddata();
		});	
		
		
		//导出
		$("#btnExport").on('click', function () {
			if(jsp == "ManufacturingJSP"){
				$("#ManufacturingPerformancelist").tableExport({
				    headings: true, 
				    footers: true, 
				    formats: ["xls"],
				    fileName: "data",
				    bootstrap: true,
				    position: "bottom",
				    ignoreRows: null,
				    ignoreCols: null
				});   
			}else{
				$("#EngineeringPerformancelist").tableExport({
				    headings: true, 
				    footers: true, 
				    formats: ["xls"],
				    fileName: "data",
				    bootstrap: true,
				    position: "bottom",
				    ignoreRows: null,
				    ignoreCols: null
				});   
			}
		});	
		
		$("#btnSave").click(function(){
			save();
		});
		
		
		$(document).on("dblclick","td",function(){

			if($(this).children("input").length > 0) 
				   return false;

			var td = $(this);

			var changeTd_ID = (jsp == "ManufacturingJSP"?9:10);
			var trindex = td.parents("tr").index() + 1;
			var ope_id= td.parent().children("td:eq(1)").html();
			
			changeArray.push(trindex);

			var tdindex = td.parents("tr").find("td").index($(this));
			if(tdindex == changeTd_ID){
				var td = $(this);  
			    //保存初始值  
			    var tdText = $(this).text();  
			    //给td设置宽度和给input设置宽度并赋值  
			    var input = $("<input type='text' />");
			    td.text("");
			    input 
				   .width(td.width()-40)                   //设置文本框宽度与td相同 
				   .height(td.height()) 
				   .val(tdText) 
				   .appendTo(td)                         //把创建的文本框插入到tdObj子节点的最后
				   .trigger("focus")                        //用trigger方法触发事件 
				   .trigger("select"); 
			    
			    //失去焦点的时候重新赋值  
			    var inputDom = $(this).find("input");  
			    inputDom.blur(function(){  
			    		
			    	var error = false;
			    	
			    	var newText = $(this).val();  
			    	
			    	if (isNaN(newText)){   //用于检查其参数是否是非数字值  
			            alert("必须输入数字！");
			            td.html(tdText); 
			            return;
			        }
			    	
			    	/*if(newText>30) {
						alert("工作纪律最高分是30,请确认！");
						error = true;
					}*/
			    	
			       
			       
			        $(this).remove();  
			        
			        if(error){
			        	td.text(tdText);
			        }else{
			        	td.text(newText);
			        }
			        
			        if(newText != tdText){
				    	isChange = true;
				    }
			        
			    });
			}



			});
		
		
		
});
	
	
